package application.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
public class DynamicRoutingFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+2;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //获取request对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        //获取参数
        String rediskey = request.getParameter("rediskey");
        //判断
        if (rediskey!=null&&rediskey.equalsIgnoreCase("customer")) {
            currentContext.put(FilterConstants.SERVICE_ID_KEY, "customer-v1");
            currentContext.put(FilterConstants.REQUEST_URI_KEY, "/getCustomer");
        }else if(rediskey!=null&&rediskey.equalsIgnoreCase("search")){
            currentContext.put(FilterConstants.SERVICE_ID_KEY, "search");
            currentContext.put(FilterConstants.REQUEST_URI_KEY, "/search/1");
        }
        return null;
    }
}
